--
-- Author: shunguo.chen
-- Date: 2018-04-18 14:36:27
--
-- GuideView 	引导界面
--

local GuideRender = import(".renders.GuideRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

function ClassRef:initRoot()

end

function ClassRef:init()
	self:setBackGroundColorOpacity(0)

	self._render = GuideRender.create()
	self:addChild(self._render, 100)
end

function ClassRef:show()
	self:setVisible(true)
	self._render:show()
end

-- guide Guide
-- params = {
--  scene = "MountView",
--  sceneSelf = 方法执行主体,
--  getFunc = func
-- }    触发数据
function ClassRef:refresh(guide, params)
	self._guide = guide
	self._params = params

	-- 支线任务引导/界面触发引导 不需要屏蔽点击
	if guide:isCanCancel() then
		self:setTouchEnabled(false)
		self:setSwallowTouches(false)
	end

	if guide:isPointTrigger() then
		-- 屏幕坐标点
		local render = self._render
		render:setStartData(params)
		render:setPosition(guide.triggerPos)
		render:setData(guide)
		render:onBtnClick(function(sender)
			executeFunc(self._onCloseView)
		end)
	else
		-- ui node
		local sceneSelf = params.sceneSelf
		local getFunc = params.getFunc
		local node
		if guide.triggerType == 1 then
			node = executeFunc(getFunc, sceneSelf, guide.id, guide.triggerFuncId)
		else
			node = executeFunc(getFunc, sceneSelf, guide.id, guide.triggerNpcId)
		end

		local parent = node:getParent()
		local pos = cc.p(node:getPosition())
		local zorder = node:getLocalZOrder()
		local touchAble = false
		if node.isTouchEnabled then
			touchAble = node:isTouchEnabled()
		end

		local worldPos = parent:convertToWorldSpace(pos)
		node:retain()
		node:removeFromParent(false)
		node:setPosition(worldPos)
		self:addChild(node)
		node:release()

		if not guide:isTouchClose() and touchAble then
			node:setTouchEnabled(false)
		end

		local rect = node:getBoundingBox()

		-- 补丁，修正移动引导位置
		local posX, posY
		if guide.id == 101 then
			local radius = node:getChildByName("radius")
			local box = radius:getBoundingBox()
			local wp = radius:convertToWorldSpace(cc.p(0,0))
			posX = wp.x + box.width/2
			posY = wp.y + box.height/2
		else
			posX = rect.x + rect.width/2
			posY = rect.y + rect.height/2
		end

		-- dump(pos, "pos")
		-- dump(worldPos, "worldPos")
		-- dump(rect, "rect")

		self._node = node
		self._parent = parent
		self._pos = pos
		self._zorder = zorder
		self._touchAble = touchAble

		local render = self._render
		render:setSelectRect(rect)
		render:setStartData(params)
		render:setPosition(posX, posY)
		render:setData(guide)
		render:onBtnClick(function(sender)
				executeFunc(self._onCloseView)
			end)
	end
end

function ClassRef:getGuide()
	return self._guide
end

function ClassRef:getStartData()
	return self._params
end

-- node返回原parent
function ClassRef:nodeReturn()
	local guide = self._guide
	if not guide:isPointTrigger() and 
		self._node and not tolua.isnull(self._node) and 
		self._parent and not tolua.isnull(self._parent) then

		local node = self._node
		node:retain()
		node:removeFromParent(false)
		if not guide:isTouchClose() and touchAble then
			node:setTouchEnabled(true)
		end
		self._parent:addChild(node)
		node:setPosition(self._pos)
		self._parent:reorderChild(node, self._zorder)
		node:release()
	end

	self._node = nil
	self._parent = nil
end

return ClassRef
